package com.pagepub.expo.manager;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.pagepub.expo.dao.AppFunctionDao;
import com.pagepub.expo.dao.AppRoleDao;
import com.pagepub.expo.dao.AppUserDao;
import com.pagepub.expo.entity.AppFunction;
import com.pagepub.expo.entity.AppRole;
import com.pagepub.expo.entity.AppUser;
@Service
@Transactional
@RemotingDestination(channels = { "my-amf", "my-secure-amf" })
public class AppUserMng{
	
	@Autowired
	private AppUserDao appUserDao;
	
	@Autowired
	private AppRoleDao appRoleDao;
	
	
	@Autowired
	private AppFunctionDao appFunctionDao;


	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	
	public AppUser findByName(String userName){
		return appUserDao.findByUserName(userName);
	}

	
	public List<AppFunction> getUserSysMenuByUserId(Long userid){
		List<AppRole> roles = appRoleDao.findByUserId(userid);
		return appFunctionDao.findByRoleId(roles);
	}
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<AppUser> findAll() {
		return appUserDao.findAll();
	}
	


	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public int getCount(Map<String, String> param){
		return appUserDao.getCount(param);
	}
	
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<AppUser> findByPage(int currentPage,int pageSize,Map<String, String> param){
		return appUserDao.findByPage(currentPage, pageSize, param);
	}


	@RemotingInclude
	public AppUser add(AppUser appUser) {
		appUserDao.insert(appUser);
		return appUserDao.findById(appUser.getUserId());
	}

	@RemotingInclude
	public Long remove(Long id) {
		appUserDao.delete(id);
		return id;
	}

	@RemotingInclude
	public AppUser update(AppUser appUser) {
		appUserDao.update(appUser);
		return appUserDao.findById(appUser.getUserId());
	}	
}
